<?php

namespace Common\Model;

use Think\Model;

class IntroModel extends Model
{
    protected $_validate = array(
        array('title', 'require', '标题必须填写！', 1),
        array('alias', 'require', '别名必须填写！', 1),
    );

    protected $_auto = array(
        array('addtime', 'time', 1, 'function'),
    );

    /**
     * 获取列表
     *
     * @param        $map
     * @param bool   $page
     * @param string $sort
     * @param string $num
     * @return mixed
     */
    public function get_list($map, $page = false, $sort = '', $num = '')
    {
        $limit = $num ? $num : 10;
        if (empty($sort)) {
            $sort = 'sort DESC,id DESC';
        }
        if ($page) {
            $count    = $this->where($map)->count();
            $Page     = pager($count, $limit);
            $limit    = $Page->firstRow . ',' . $Page->listRows;
            $page_str = $Page->fshow();
        }
        $result = $this->where($map)->order($sort)->limit($limit)->select();
        foreach ($result as $key => $v) {

            $v['url'] = RU('intro/view', array('alias' => $v['alias']));
            $list[]   = $v;
        }
        $return['list'] = $list;
        if ($page) {
            $return['count'] = $count;
        } else {
            $return['count'] = count($list);
        }
        $return['page'] = $page_str;
        //print_r($return);
        return $return;
    }

    /**
     * 获取内容
     *
     * @param string $id
     * @param string $alias
     * @return mixed
     */
    public function get_intro_one($id = '', $alias = '')
    {
        if (!empty($id)) {
            $map['id'] = intval($id);
        }
        if (!empty($alias)) {
            $map['alias'] = trim($alias);
        }
        $info            = $this->where($map)->find();
        $info['content'] = htmlspecialchars_decode($info['content'], ENT_QUOTES);
        return $info;
    }

    /**
     * 更新点击数量
     *
     * @param string $id
     * @return bool
     */
    public function inc_click($id = '')
    {
        $map['id'] = intval($id);
        $this->where($map)->setInc('click', 1);
        return true;
    }

}